package edu.ccut.saturn.component.loader.data;

import java.util.List;

import org.dom4j.Element;

import edu.ccut.saturn.component.ISaturnDataSqlItemInfo;
import edu.ccut.saturn.component.impl.sql.IterateItemInfo;

public class IterateElementLoader extends AbstractSqlItemInfoLoader {

	private static final String NODE_KEY_ITERATE_OPEN = "open";

	private static final String NODE_KEY_ITERATE_CLOSE = "close";

	private static final String NODE_KEY_ITERATE_CONJUNCTION = "conjunction";

	@Override
	public ISaturnDataSqlItemInfo loadSqlItemInfo(Object element)
			throws Exception {

		if (element instanceof Element) {
			String prepend = ((Element) element)
					.attributeValue(NODE_KEY_PREPEND);

			String property = ((Element) element)
					.attributeValue(NODE_KEY_PROPERTY);

			String openStr = ((Element) element)
					.attributeValue(NODE_KEY_ITERATE_OPEN);

			String closeStr = ((Element) element)
					.attributeValue(NODE_KEY_ITERATE_CLOSE);

			String conjunctionStr = ((Element) element)
					.attributeValue(NODE_KEY_ITERATE_CONJUNCTION);

			IterateItemInfo iterateItemInfo = new IterateItemInfo(prepend,
					property, openStr, closeStr, conjunctionStr);

			if (((Element) element).content().size() > 0) {
				List<ISaturnDataSqlItemInfo> sqlItemInfos = getSqlItemInfos(((Element) element)
						.content());

				iterateItemInfo.setSubSqlItemInfos(sqlItemInfos);
			}
			return iterateItemInfo;
		}
		return null;
	}

}
